/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.app.utils.security;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Santiago
 */
public class UrlRewriteFilter implements Filter{

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest srequest = (HttpServletRequest) request;
        HttpServletResponse sresponse = (HttpServletResponse) response;
 
        String url = srequest.getRequestURI().trim();
 
  
        if (url.contains("/resources/api.json")) {
            String newUrl = url.substring(0, url.indexOf(".json"));
            StringBuilder forward = new StringBuilder();                        
            forward.append(newUrl);
            forward.append("?");
            forward.append(srequest.getQueryString());
            
            sresponse.sendRedirect(forward.toString());
             
             
        } else {            
            chain.doFilter(request, response);
        }
  }

  @Override
  public void destroy() {
  }
  
}
